/**
 * Desc:抓取股票数据服务子类
 * Author：Kevin
 * CreatedOn: 2021/4/21
 * Version: 1.0
 */


const EtlServiceBase = require('./etlservicebase');

/**
 * 市场数据抓取服务类
 */
class MarketService extends EtlServiceBase {
    constructor(dataRoot) {
        super(dataRoot);

        this.append = true;
        this.dataTag = 'diff'
        this.id = '1';
        this.url = "http://20.push2.eastmoney.com/api/qt/clist/get";

        this.params['cb'] = 'jQuery112408109097191366572_1618973809341';
        this.params['pn'] = 1;
        this.params['pz'] = 20;
        this.params['po'] = '1';
        this.params['np'] = '1';
        this.params['ut'] = 'bd1d9ddb04089700cf9c27f6f7426281';
        this.params['fltt'] = '2';
        this.params['invt'] = '2';
        this.params['fid'] = 'f3';
        //this.params['fs'] = 'm:0 t:6,m:0 t:13,m:0 t:80,m:1 t:2,m:1 t:23';
        this.params['fs'] = 'm:1 t:2,m:1 t:23';
        //this.params['fields'] = 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152';
        this.params['fields'] = 'f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18';

        this.fields = {
            'f2': 'price',
            'f3': 'change_rate',
            'f4': 'change_amount',
            'f5': 'volume',
            'f6': 'turnover',
            'f7': 'volatility',
            'f8': 'turnover_rate',
            'f9': 'roe',
            'f10': 'amount_rate',
            'f12': 'code',
            'f13': 'market',
            'f14': 'name',
            'f15': 'highest',
            'f16': 'lowest',
            'f17': 'open',
            'f18': 'last_close',
        };
    }
}
/**
 * 股票数据抓取服务类
 */
class StockHistoryService extends EtlServiceBase {
    /**
     * 构造函数
     * @param {string} market 市场代码
     * @param {string} sid 股票代码
     */
    constructor(market, sid, dataRoot) {
        super(dataRoot);

        this.append = false;
        this.dataTag = 'klines'
        this.id = `${market}.${sid}`;
        this.url = "http://push2his.eastmoney.com/api/qt/stock/kline/get";

        this.params['cb'] = 'jsonp1618558913680';
        this.params['klt'] = '101';
        this.params['secid'] = this.id;
        this.params['ut'] = 'fa5fd1943c7b386f172d6893dbfba10b';
        this.params['fqt'] = '1';
        this.params['beg'] = '0';
        this.params['end'] = '20500101';
        this.params['rtntype'] = '6';
        this.params['fields1'] = 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13';
        this.params['fields2'] = 'f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61';

        this.fields = {
            'f51': 'date',
            'f52': 'open',
            'f53': 'close',
            'f54': 'highest',
            'f55': 'lowest',
            'f56': 'volume',
            'f57': 'turnover',
            'f58': 'volatility',
            'f59': 'change_rate',
            'f60': 'change_amount',
            'f61': 'turnover_rate',
        };

    }
}

/**
 * 股票核心数据抓取服务
 */
class StockCoreService extends EtlServiceBase {
    constructor(market, sid, dataRoot) {
        super(dataRoot);

        this.append = true;
        this.dataTag = ''
        this.id = `${market}_stock_core`;
        this.url = 'http://push2.eastmoney.com/api/qt/stock/get';

        this.params['cb'] = 'jQuery112409943202935516526_1618985451537';
        this.params['secid'] = `${market}.${sid}`;
        this.params['ut'] = 'fa5fd1943c7b386f172d6893dbfba10b';
        this.params['fltt'] = '2';
        this.params['invt'] = '2';
        this.params['fields'] = 'f55,f62,f84,f85,f92,f173,f103,f104,f105,f108,f109,f116,f117,f126,f160,f183,f184,f185,f186,f187,f188,f190,f162,f167,f189';

        this.fields = {
            'f84': 'total', //总股本
            'f85': 'floating', //流通股
            'f92': 'neta', //每股净资产
            'f105': 'np', //净利润
            'f108': 'return', // 收益
            'f116': 'mv', //总值
            'f117': 'mv_floating', //流值
            'f162': 'pe', //PE(动)
            'f167': 'pb', //市净率
            'f173': 'roe', //ROE
            'f183': 'sales', //总营收
            'f185': 'npgr', //净利润同比
            'f186': 'gr', //毛利率
            'f187': 'npr', //净利率
            'f188': 'lr', //负债率
            'f189': 'ipo_date', //上市时间
            'f190': 'unassign', //每股未分配利润
        };
    }
}


module.exports = {
    MarketService,
    StockHistoryService,
    StockCoreService,
}